<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; 
use App\Realname;

Class AccountController extends AdminController{ 
    
    /**
     * 用户-转账记录
     * @param Request $request
     */
    public function qrPayLog(Request $request){
        $name = trim($request->input('name', ''));
        $phone = trim($request->input('phone', ''));
        $idcard = trim($request->input('idcard', ''));
        $payType = trim($request->input('paytype', ''));//1余额，2银行卡
        $start = trim($request->input('start', ''));
        $end = trim($request->input('end', ''));
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        $sql = "select c.realname,b.from_member_id,b.to_member_id,b.paymethod,b.amount,b.status,b.created_at from mega_member as a inner join mega_account_qrlog as b on a.id=b.from_member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $sqlcount = "select count(a.id) as count from mega_member as a inner join mega_account_qrlog as b on a.id=b.from_member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $param = array();
        $sqlwhere = '';
        if($idcard){
            $sqlwhere.=" and c.card_id=:card_id ";
            $param[':card_id']= $idcard;
        }
        if($name){
            $name = "%$name%";
            $sqlwhere.=" and c.realname like :realname ";
            $param[':realname']= $name;
        }
        if($phone){
            $sqlwhere.=" and a.phone=:phone ";
            $param[':phone']= $phone;
        }
        if($payType){
            $sqlwhere.=" and b.paymethod=:paymethod ";
            $param[':paymethod']= $payType;
        }
        if($start){
            $sqlwhere.= " and b.created_at >= :start_time " ;
            $param[':start_time']= $start;
        }
        if($end){
            $end.=' 23:59:59';
            $sqlwhere.= " and b.created_at <= :end_time " ;
            $param[':end_time']= $end;
        }
        
        $start = $page * $pagenumber;
        $limit = " order by b.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $resultsNew = array();
        
        foreach ($results as $item){
            $real = Realname::where('member_id', $item->to_member_id)->first();
            $item->toname = '';
            if($real){
                $item->toname = $real->realname;
            }
            $resultsNew[] = $item;
        }
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$resultsNew,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
    // 交易流水，资金变动日志 
    public function amountChangeLog(Request $request)
    {
        $name = trim($request->input('name', ''));
        $phone = trim($request->input('phone', ''));
        $idcard = trim($request->input('idcard', ''));
        $type = trim($request->input('type', ''));//1-9
        $start = trim($request->input('start', ''));
        $end = trim($request->input('end', ''));
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        $sql = "select c.realname,a.phone,c.card_id,b.from_member_id,b.to_member_id,b.type,b.remark,b.amount_money,b.add_time from mega_member as a inner join mega_account_log as b on a.id=b.member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $sqlcount = "select count(a.id) as count from mega_member as a inner join mega_account_log as b on a.id=b.member_id left join mega_realname as c on a.id = c.member_id where 1 ";
        $param = array();
        $sqlwhere = '';
        if($idcard){
            $sqlwhere.=" and c.card_id=:card_id ";
            $param[':card_id']= $idcard;
        }
        if($name){
            $name = "%$name%";
            $sqlwhere.=" and c.realname like :realname ";
            $param[':realname']= $name;
        }
        if($phone){
            $sqlwhere.=" and a.phone=:phone ";
            $param[':phone']= $phone;
        }
        if($type){
            $sqlwhere.=" and b.type=:type ";
            $param[':type']= $type;
        }
        if($start){
            $sqlwhere.= " and b.add_time >= :start_time " ;
            $param[':start_time']= strtotime($start);
        }
        if($end){
            $end.=' 23:59:59';
            $sqlwhere.= " and b.add_time <= :end_time " ;
            $param[':end_time']= strtotime($end);
        }
        
        $start = $page * $pagenumber;
        $limit = " order by b.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $resultsNew = array();
        
        foreach ($results as $item){
            $item->date = date('Y-m-d', $item->add_time);
            $resultsNew[] = $item;
        }
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$resultsNew,
            'page'=>$page
        );
        
        $this->output($data);
        
         
    }
    
}
